/**
 *  promise实例对象
 *      1. PromiseState='pending'
 *      2. PromiseResult=undefined;
 * 
 *  改变promise状态的三种途径：
 * 
 *  1. resolve(成功结果值)
 *  2. reject(失败的结果值)
 *  3. throw 失败的结果值
 */
 function Promise(executor){
    this.PromiseState = 'pending';
    this.PromiseResult = undefined;
    // 要定义成箭头函数，确保函数内this指向当前promise实例对象
    const resolve = (value)=>{ 
        // 1. 改变promise的状态为 fulfilled
        // 2. 设置结果值
        this.PromiseState = 'fulfilled';
        this.PromiseResult = value;
    }
    const reject = (reason)=>{
        // 1. 改变promise的状态为 rejected
        // 2. 设置失败的结果值
        this.PromiseState = 'rejected';
        this.PromiseResult = reason;
    }
    try{
        executor(resolve, reject);
    }catch(e){
        reject(e);
        console.error(e);
    }
}